/**
 * Created by dale on 2017/10/28.
 */
/**
 * Created by dale on 2017/10/9.
 */
var msgList;
var type;
var schoolId;
var userId;
$(document).ready(function () {
    msgList = $('#msgList');
    $('.xh-datagrid-toolbar').parseToolbar();
    type = addressUri.getParam('type');
    schoolId = addressUri.getParam('schoolId');
    userId   = addressUri.getParam('userId');
    var openType = addressUri.getParam('openType');
    msgList.initDataGrid(baseUrl + 'frontend/article/getMsg',{queryParams:getParams()}).setSelectThenEnable(['bt_list_del'],['bt_list_share','bt_list_setExc']);


    $('#bt_list_add').click(function () {
        var title ='';
        if(type==4){
            title= '发布应用';
        }else if(type==5){
            title= '发布教辅';
        }else if(type==6){
            title= '发布计划';
        }else if(type ==7){
            title = '运营案例';
            window.top.xhTop.showModal({
                title:title,
                content: '<iframe src="'+baseUrl+'frontend/article/msgForm?iArtType='+type+'&schoolId='+schoolId+'&userId='+userId+' "  style="width: 100%;;border-style: none;padding: 0px;height:500px;"  scrolling="yes"  ></iframe>',
                width:975,
                backClose:false
            });
            return false;
        }
        window.top.xhTop.showModal({
            title:title,
            content: '<iframe src="'+baseUrl+'frontend/article/msgForm?iArtType='+type+'"  style="width: 100%;;border-style: none;padding: 0px;height:500px;"  scrolling="yes"  ></iframe>',
            width:975,
            backClose:false
        });
    });

    if(type == 7 && schoolId == null){
        $('#bt_list_add,#bt_list_del').hide();
        msgList.datagrid('showColumn' , 'sSchoolName');
    }else if (type == 7  && schoolId != null){
        $('#select_school').next('.combo').hide();
    }

    if (type == 7){
        $('#list_search_area').combobox({  //区域选择
            onSelect:function (rec) {
                var pid = rec.value;
                $('#list_search_city').combobox({
                    url:baseUrl+'school_examine/getDeps/'+pid,
                    valueField: 'id',
                    textField: 'text'
                });
            }
        });
        $('.textbox combo').attr('width', '100');
    }



    $('#bt_list_share').click(function () {
        if(xhExtra.buttonDisabled($(this))){
            return false;
        }
        if(schoolId == null){
            var selections = msgList.datagrid('getSelections');
            if(selections.length > 1 || selections.length <= 0 ){
                window.top.xhTop.showError('错误','请选择一个学校进行操作');
                return false;
            }
        }
        var nowSchoolId = schoolId == null ? msgList.getDataGridSelectedField('iSchoolId') : schoolId;
        var iArtId=msgList.getDataGridSelectedField('iArtId');
        window.top.xhTop.showModal({
            title: '新建分享',
            content: '<iframe  src="'+baseUrl+'school_share/caseShareForm/'+nowSchoolId+'?iArtId='+iArtId+'"  style="width: 100%;;border-style: none;padding: 0px;height:300px"  scrolling="yes"   ></iframe>',
            button:'提交',
            backClose:false
        });
    });
    if(type == 7 && $('#bt_list_exmenu').length > 0){
        var  $btn_menu1=$('#bt_list_exmenu').menubutton({
            iconCls: 'fa fa-user',
            menu: '#mm'
        });
        $($btn_menu1.menubutton('options').menu).menu({
            onClick: function (item) {
                //item 的相关属性参见API中的menu
                if(schoolId == null){
                    var selections = msgList.datagrid('getSelections');
                    if(selections.length > 1 || selections.length <= 0 ){
                        window.top.xhTop.showError('错误','请选择一个学校进行操作');
                        return false;
                    }
                }
                var nowSchoolId = schoolId == null ? msgList.getDataGridSelectedField('iSchoolId') : schoolId;
                if(item.text == '案例打分'){
                    window.top.xhTop.showModal({
                        title: '案例打分',
                        content: '<iframe  src="'+baseUrl+'school_share/caseGrade?schoolId='+nowSchoolId+'"  style="width: 100%;;border-style: none;padding: 0px;height:300px"  scrolling="yes"   ></iframe>',
                        button:'提交',
                        backClose:false
                    });
                }else if(item.text == '查看案例打分'){
                    window.top.xhTop.showModal({
                        title: '查看案例打分',
                        content: '<iframe  src="'+baseUrl+'school_share/gradeList?schoolId='+nowSchoolId+'"  style="width: 100%;;border-style: none;padding: 0px;height:300px"  scrolling="yes"   ></iframe>',
                        backClose:false
                    });
                }

            }
        });
    }

    if (type == 7 && $('#bt_list_levelMenu').length > 0){
        var  $btn_menu2=$('#bt_list_levelMenu').menubutton({
            iconCls: 'fa fa-bars',
            menu: '#mm2'
        });
        $($btn_menu2.menubutton('options').menu).menu({
            onClick: function (item) {
                //item 的相关属性参见API中的menu
                var iArtId=msgList.getDataGridSelectedField('iArtId');
                if ( iArtId  == '') {
                    window.top.xhTop.showError('错误' , '请先选择一个案例');
                    return false;
                }
                if (item.text == '优秀') {
                    xhExtra.askChangeStatus('article/changeLevel',{id:iArtId,level:3},null,'确认要将该案例评为优秀案例吗？',function (res) {
                        window.top.xhTop.showSuccess(res.msg);
                        msgList.dataGridReload();
                    })
                } else if(item.text == '良好'){
                    xhExtra.askChangeStatus('article/changeLevel',{id:iArtId,level:2},null,'确认要将该案例评为良好案例吗？',function (res) {
                        window.top.xhTop.showSuccess(res.msg);
                        msgList.dataGridReload();
                    })
                } else if (item.text == '一般') {
                    xhExtra.askChangeStatus('article/changeLevel',{id:iArtId,level:1},null,'确认要将该案例评为一般案例吗？',function (res) {
                        window.top.xhTop.showSuccess(res.msg);
                        msgList.dataGridReload();
                    })
                }
            }
        });
    }



    // $('#bt_list_setExc').click(function () {
    //     if(xhExtra.buttonDisabled($(this))){
    //         return false;
    //     }
    //     var iArtId=msgList.getDataGridSelectedField('iArtId');
    //     xhExtra.askChangeStatus('school_share/setExcellent',{iArtId:iArtId},null,'是否设置为优秀案例?',function (res) {
    //         window.top.xhTop.sendSuccessMsg(res.msg);
    //     })
    // });


    //搜索
    $('#bt_list_search').click(function () {
        var key = xhExtra.getValue($('#list_search'));
        var schoolId = schoolId == null ?  xhExtra.getValue($('#select_school')) : schoolId ;
        var area = xhExtra.getValue($('#list_search_area'));
        var city = xhExtra.getValue($('#list_search_city'));
        msgList.addDataGridParams({key:key,schoolId:schoolId,type:type,area:area,city:city});
    });


    $('#bt_list_seeShare').click(function () {

        window.top.xhTop.showModal({
            title: '案例分享',
            content: '<iframe  src="'+baseUrl+'school_share/caseShareList?schoolId='+schoolId+'"  style="width: 100%;;border-style: none;padding: 0px;height:500px"  scrolling="yes"   ></iframe>',
            width:900,
            backClose:false
        });
    });



    $('#bt_list_del').click(function () {
        if(xhExtra.buttonDisabled($(this))){
            return false;
        }
        var ids=msgList.getDataGridSelectedFields('iArtId');
        myWindow.initPopUp({title: '确定要删除么?', text: '这些文章将被删除!'},function () {
            $.ajax({
                url:baseUrl+'frontend/article/delMsg',
                data:{ids:ids,type:type},
                type:'POST',
                success:function (data) {
                    window.top.swal(
                        data['msg'],
                        '',
                        'success'
                    );
                    msgList.dataGridReload();
                },
                error:function (data) {
                    window.top.swal(
                        data['responseText'],
                        '',
                        'success'
                    );
                }
            });
        });
    });


    DataGrid.onLoadSuccess = function () {
      msgList.dataGridClear();
    };

    DataGrid.onDblClickRow=function (rowIndex, rowData) {
        if(openType!=null){
            var iArtId = rowData['iArtId'];
            var iArtType = rowData['iArtType'];
            var title  = '';
            if(iArtType == 4 ){
                title = '应用详情';
            }else if(iArtType == 5) {
                title = '教辅详情';
            }else if(iArtType == 6){
                title = '计划详情';
            }else if(iArtType == 7){
                title = '运营案例';
            }
            window.top.xhTop.showModal({
                title:title,
                content: '<iframe src="'+baseUrl+'mobile/home/msgDetail?iArtId='+iArtId+'"  style="width: 100%;;border-style: none;padding: 0px;height:500px"  scrolling="yes"   ></iframe>',
                backClose:false,
                button:true,
            },function () {
                var dom = $('.modal-footer',window.top.document);
                dom.empty();
                dom.append('<div class="form-group" style="margin-left: 20px;margin-bottom:5px;background-color: #ececec;border: solid 1px #E4e4e4;border-top: 0px;">\n' +
                    '                <input id="readUrl" value="'+window.location.origin+baseUrl+'mobile/home/msgDetail?iArtId='+iArtId+'" readonly="" style="width: 100%;height:30px;background-color: transparent;border: 0px;">\n' +
                    '       </div>');
            });
        }else {
            var iArtId = rowData['iArtId'];
            var iArtType = rowData['iArtType'];
            var title  = '';
            if(iArtType == 4 ){
                title = '发布应用';
            }else if(iArtType == 5) {
                title = '发布教辅';
            }else if(iArtType == 6){
                title = '发布计划';
            }else if(iArtType == 7){
                title = '运营案例';
            }
            var schoolId = schoolId == null ? rowData['iSchoolId'] : schoolId;
            window.top.xhTop.showModal({
                title:title,
                content: '<iframe src="'+baseUrl+'frontend/article/msgForm?iArtType='+iArtType+'&iArtId='+iArtId+'&schoolId='+schoolId+'&userId='+userId+'"  style="width: 100%;;border-style: none;padding: 0px;height:500px;"  scrolling="yes"   ></iframe>',
                width:975,
                backClose:false,
                button:true,
            },function () {
                var dom = $('.modal-footer',window.top.document);
                dom.empty();
                dom.append('<div class="form-group" style="margin-left: 20px;margin-bottom:5px;background-color: #ececec;border: solid 1px #E4e4e4;border-top: 0px;">\n' +
                    '                <input id="readUrl" value="'+window.location.origin+baseUrl+'mobile/home/msgDetail?iArtId='+iArtId+'" readonly="" style="width: 100%;height:30px;background-color: transparent;border: 0px;">\n' +
                    '       </div>');
            });
            // window.top.xhTop.createNewTab('编辑文章',baseUrl+'frontend/article/msgForm?iArtType='+iArtType+'&iArtId='+iArtId);
            return false;
        }
    }
});

function getParams() {
    // var key=xhExtra.getValue($('#list_search'));
    var params =null;
    if(type == 7){
        params = {type:type,schoolId:schoolId,userId:userId};
    }else {
        params = {type:type};
    }
    return params;
}